Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Common
Imports KanBan.Pgd.Presentation.Controls.TabControl
Imports KanBan.Pgd.Presentation.Session
Imports System.Threading
Imports System.Resources
Imports KanBan.Pgd.Infrastructure.Notifications
Imports KanBan.Pgd.Security

Public Class Pgd

    Private WithEvents timer As System.Windows.Forms.Timer

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        AddHandler ClientSession.UserLoggedOut, AddressOf OnUserLoggedOut
        AddHandler ClientSession.ApplicationCultureChanged, AddressOf OnApplicationCultureChanged
        AddHandler Me.TabContainer.TabClosed, AddressOf OnTabClosed

        UIContextualizationHelper.HideNonPublicMenuOptions(Me.MainMenuItems)

        Dim cultures As IList(Of ApplicationCulture) = PgdSettings.GetSupportedApplicationCultures()
        Me.AddCultures(cultures)

    End Sub

    Private Sub OnUserLoggedOut()

        UIContextualizationHelper.HideNonPublicMenuOptions(Me.MainMenuItems)

        Me.ContextualizeToUserLoggedOut()


    End Sub

    Private Sub ContextualizeToUserLoggedOut()

        Me.UserLoggedInLabel.Visible = False
        Me.LogInAsAnotherUserToolStripMenuItem.Text = "Login"

        Me.TabContainer.TabPages.Clear()

    End Sub

    Private Sub OnApplicationCultureChanged()


        Thread.CurrentThread.CurrentCulture = ClientSession.CurrentCulture
        Thread.CurrentThread.CurrentUICulture = ClientSession.CurrentCulture

        Dim th As New UITranslationHelper(New ResourceManager(GetType(Pgd)))
        th.Translate(Me.FormControls)


    End Sub

    Private Sub OnTabClosed(ByVal sender As ExtendedTabControl, ByVal e As CloseEventArgs)

        Me.TabContainer.TabPages.RemoveAt(e.TabIndex)


    End Sub

#Region "IPgdView"

    Public ReadOnly Property MainMenuItems() As ToolStripItemCollection
        Get
            Return mspMainMenu.Items
        End Get
    End Property

    Public ReadOnly Property FormControls() As Control.ControlCollection
        Get
            Return Me.Controls
        End Get
    End Property

    Public ReadOnly Property UserLoggedInLabel() As ToolStripLabel
        Get
            Return Me.tsplblUserLoggedIn
        End Get
    End Property

    Public ReadOnly Property LogInAsAnotherUserToolStripMenuItem() As ToolStripMenuItem
        Get
            Return Me.tsmiLogInAsAnotherUser
        End Get
    End Property

    Public ReadOnly Property ChildForms() As Form()
        Get
            Return Me.MdiChildren
        End Get
    End Property

    Public Property UserLogged() As String
        Get
            Return tsplblUserLoggedIn.Text
        End Get
        Set(ByVal value As String)
            tsplblUserLoggedIn.Text = value
        End Set
    End Property

    Public ReadOnly Property TabContainer() As ExtendedTabControl
        Get
            Return Me.MainTabControl
        End Get
    End Property

    Private Sub OnManageSearchRequestsClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)

        If MainTabControl.TabPages.ContainsKey(PGDTabs.ManageSearchRequests) Then
            MainTabControl.TabPages.RemoveByKey(PGDTabs.ManageSearchRequests)
        End If

    End Sub

    Public Sub OnManageUsersClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)

        If MainTabControl.TabPages.ContainsKey(PGDTabs.ManageUsers) Then
            MainTabControl.TabPages.RemoveByKey(PGDTabs.ManageUsers)
        End If

    End Sub

    Public Sub OnManagePerformanceEvaluationsClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)

        If MainTabControl.TabPages.ContainsKey(PGDTabs.ManagePerformanceEvaluations) Then
            MainTabControl.TabPages.RemoveByKey(PGDTabs.ManagePerformanceEvaluations)
        End If

    End Sub

    Public Sub OnManageGroupsClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)

        If MainTabControl.TabPages.ContainsKey(PGDTabs.ManageGroups) Then
            MainTabControl.TabPages.RemoveByKey(PGDTabs.ManageGroups)
        End If

    End Sub

    Public Sub OnEventLogClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)

        If MainTabControl.TabPages.ContainsKey(PGDTabs.EventLog) Then
            MainTabControl.TabPages.RemoveByKey(PGDTabs.EventLog)
        End If

    End Sub

    Public Sub OnManageCandidatesClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)

        If MainTabControl.TabPages.ContainsKey(PGDTabs.ManageCandidates) Then
            MainTabControl.TabPages.RemoveByKey(PGDTabs.ManageCandidates)
        End If

    End Sub

    Public Sub AddCultures(ByVal cultures As IEnumerable(Of ApplicationCulture))

        For Each ac As ApplicationCulture In cultures

            Dim item As New ToolStripButton()
            item.Name = ac.Name
            item.Text = ac.DisplayName

            tsddbLanguages.DropDownItems.Add(item)
            If ac.IsDefault Then tsddbLanguages.DropDownItems(item.Name).Select()

        Next

    End Sub

    Public Sub SetUnreadMessagesCount(ByVal count As Integer)

        If count <= 0 Then
            Me.InboxCountToolStripMenuItem.Visible = False
        Else
            Me.InboxCountToolStripMenuItem.Visible = True
        End If

        Me.InboxCountToolStripMenuItem.Text = String.Format("({0})", count)
    End Sub

#End Region

    Private Sub ManageUsersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ManageUsersToolStripMenuItem.Click
        Me.ManageUsers()
    End Sub

    Public Sub ManageUsers()

        Dim form As New ManageUsers()
        form.TopLevel = False
        form.Dock = DockStyle.Fill

        UIHelper.ResolveTabPage(Me.TabContainer, form, PGDTabs.ManageUsers, "Manage Users")

        AddHandler form.FormClosed, AddressOf Me.OnManageUsersClosed

        form.Show()

    End Sub

    Private Sub ManageGroupsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ManageGroupsToolStripMenuItem.Click
        Me.ManageGroups()
    End Sub

    Public Sub ManageGroups()

        Dim form As New ManageGroups()
        form.TopLevel = False
        form.Dock = DockStyle.Fill

        UIHelper.ResolveTabPage(Me.TabContainer, form, PGDTabs.ManageGroups, "Manage Groups")

        AddHandler form.FormClosed, AddressOf Me.OnManageGroupsClosed

        form.Show()

    End Sub

    Private Sub Pgd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ShowLogin()
    End Sub

    Public Sub ShowLogin()


        Dim form As New Login()

        AddHandler form.UserIsLogged, AddressOf OnUserLoggedIn

        form.ShowDialog()

    End Sub

    Private Sub OnUserLoggedIn()

        UIContextualizationHelper.ContextualizeMenuOptions(Me.MainMenuItems)

        Me.UserLogged = ClientSession.UserCredentials.UserName

        Me.ContextualizeToUserLoggedIn()

        Me.RedirectToLandingPage()

        Me.UpdateInboxCount()

        timer = New System.Windows.Forms.Timer()
        timer.Interval = 30000
        timer.Start()

    End Sub

    Private Sub timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick
        Me.UpdateInboxCount()
    End Sub

    Private Sub UpdateInboxCount()
        Dim userId As Guid = ClientSession.UserCredentials.Id
        Dim count As Integer = Notifier.GetAllUnreadNotificationsCount(userId)
        Me.SetUnreadMessagesCount(count)
    End Sub

    Private Sub RedirectToLandingPage()

        If ClientSession.UserCredentials.HasPermission(PermissionList.ReadSearchRequestes) Then

            Me.ManageSearchRequests()

        End If

    End Sub

    Public Sub ManageSearchRequests()

        Dim form As New ManageSearchRequests()
        form.TopLevel = False
        form.Dock = DockStyle.Fill

        UIHelper.ResolveTabPage(Me.TabContainer, form, PGDTabs.ManageSearchRequests, "Manage Search Requests")

        AddHandler form.FormClosed, AddressOf Me.OnManageSearchRequestsClosed

        form.Show()

    End Sub


    Private Sub ContextualizeToUserLoggedIn()

        Me.UserLoggedInLabel.Visible = True
        Me.LogInAsAnotherUserToolStripMenuItem.Text = "Login as another user"

    End Sub

    Private Sub ManageSearchRequestsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ManageSearchRequestsToolStripMenuItem.Click
        Me.ManageSearchRequests()
    End Sub

    Private Sub LogoffToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiLogOff.Click
        Me.LogOff()
    End Sub

    Public Sub LogOff()

        ClientSession.Dispose()

    End Sub

    Private Sub LogInAsAnotherUserToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiLogInAsAnotherUser.Click
        Me.ShowLogin()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub EventLogToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EventLogToolStripMenuItem.Click
        Me.ShowEventLog()
    End Sub

    Public Sub ShowEventLog()


        Dim form As New ShowEventLog()
        form.TopLevel = False
        form.Dock = DockStyle.Fill

        UIHelper.ResolveTabPage(Me.TabContainer, form, PGDTabs.EventLog, "Event Log")

        AddHandler form.FormClosed, AddressOf Me.OnEventLogClosed

        form.Show()

    End Sub

    Private Sub InboxToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InboxToolStripMenuItem.Click
        Me.ShowInbox()
    End Sub

    Public Sub ShowInbox()


        Dim form As New ShowNotifications()

        form.ShowDialog()

    End Sub

    Private Sub ManageCandidatesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ManageCandidatesToolStripMenuItem.Click
        Me.ManageCandidates()
    End Sub

    Public Sub ManageCandidates()

        Dim form As New ManageCandidates()
        form.TopLevel = False
        form.Dock = DockStyle.Fill

        UIHelper.ResolveTabPage(Me.TabContainer, form, PGDTabs.ManageCandidates, "Manage Candidates")

        AddHandler form.FormClosed, AddressOf Me.OnManageCandidatesClosed

        form.Show()

    End Sub

    Public Sub ManagePerformanceEvaluations()

        Dim form As New ManagePerformanceEvaluations()
        form.TopLevel = False
        form.Dock = DockStyle.Fill

        UIHelper.ResolveTabPage(Me.TabContainer, form, PGDTabs.ManagePerformanceEvaluations, "Manage Performance Evaluations")

        AddHandler form.FormClosed, AddressOf Me.OnManagePerformanceEvaluationsClosed

        form.Show()

    End Sub

    Public Sub ChangeApplicationCulture(ByVal name As String)

        Dim selectedCulture As New ApplicationCulture(name, False)

        ClientSession.CurrentCulture = selectedCulture

    End Sub

    Private Sub PerformanceEvaluationsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PerformanceEvaluationsToolStripMenuItem.Click
        Me.ManagePerformanceEvaluations()
    End Sub

    Private Sub tsddbLanguages_DropDownItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles tsddbLanguages.DropDownItemClicked

        Me.ChangeApplicationCulture(e.ClickedItem.Name)

    End Sub

    Private Sub EditMyProfileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditMyProfileToolStripMenuItem.Click

        Me.EditMyProfile()

    End Sub

    Private Sub EditMyProfile()

        Dim form As New EditUserProfile(ClientSession.UserCredentials.Id)
        form.TopLevel = False
        form.Dock = DockStyle.Fill

        UIHelper.ResolveTabPage(Me.TabContainer, form, PGDTabs.EditUserProfile, "Edit My Profile")

        AddHandler form.FormClosed, AddressOf Me.OnEditUserProfileClosed

        form.Show()

    End Sub

    Private Sub OnEditUserProfileClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)

        If MainTabControl.TabPages.ContainsKey(PGDTabs.EditUserProfile) Then
            MainTabControl.TabPages.RemoveByKey(PGDTabs.EditUserProfile)
        End If

    End Sub

End Class